package com.mty.cc.controller;


import com.mty.cc.po.Admin;
import com.mty.cc.po.Notice;
import com.mty.cc.service.AdminService;
import com.mty.cc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/notice")
class NoticeController {


    @Autowired(required=false)
    private NoticeService noticeService;

    @Autowired(required=false)
    private AdminService adminService;

    /*
     * @description: 跳转到首页
     * @author: mty
     */
    @RequestMapping("/noticeIndex")
    public String noticeIndex(HttpServletRequest request,Model model) throws Exception{
        List<Notice> noticeList = noticeService.queryByAll();
        int total = noticeList.size();
        HttpSession session = request.getSession();
        String type = session.getAttribute("type").toString();
        model.addAttribute("noticeList", noticeList);
        model.addAttribute("total", total);
        model.addAttribute("type", type);
        return "notice/NoticeIndex";
    }

    /*
     * @description:进入修改
     * @author: mty
     */
    @RequestMapping("/noticeEdit/{id}")
    public ModelAndView  noticeEdit(@PathVariable("id") String id,Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        Notice notice = noticeService.queryById(id);
        model.addAttribute("notice", notice);
        mv.setViewName("notice/NoticeEdit");
        return mv;
    }

    /*
     * @description:修改
     * @author: mty
     */
    @RequestMapping("/noticeEditSubmit")
    public String  noticeEditSubmit(Notice notice,Model model,HttpServletRequest request) throws Exception{
        try{
            noticeService.update(notice);
            request.setAttribute("msg", "修改成功！");
            return "notice/NoticeEdit";
        }catch (Exception e){
            request.setAttribute("msg", "修改失败！");
            return "notice/NoticeEdit";
        }
    }

    /*
     * @description:进入添加
     * @author: mty
     */
    @RequestMapping("/noticeAdd")
    public String  noticeAdd(Model model,HttpServletRequest request) throws Exception{
        return "notice/NoticeAdd";
    }

    /*
     * @description:增加
     * @author: mty
     */
    @RequestMapping("/noticeAddSub")
    public String  noticeAddSub(Notice notice,Model model,HttpServletRequest request) throws Exception{
        try{
            HttpSession session = request.getSession();
            String id = session.getAttribute("id").toString();
            Admin admin = adminService.queryById(id);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(date);
            notice.setTime(time);
            notice.setPush(admin.getUsername());
            noticeService.add(notice);
            request.setAttribute("msg", "添加成功！");
            return "notice/NoticeAdd";
        }catch (Exception e){
            request.setAttribute("msg", "添加失败！");
            return "notice/NoticeAdd";
        }
    }

    /*
     * @description:根据ID删除
     * @author: mty
     */
    @RequestMapping("/noticeDel/{id}")
    public ModelAndView noticeDel(@PathVariable("id") String id,HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        noticeService.deleteById(id);
        mv.setViewName("notice/NoticeIndex");
        return mv;
    }


}